package com.gin.kotlin.no_gay_alliance.provider.ngajs

import com.gin.nga.api.ResourceApi
import com.gin.nga.response.resource.bbscode.BbsCodeBody
import com.gin.nga.response.resource.bbscode.EmoteGroup
import java.lang.ref.WeakReference
import java.util.concurrent.TimeUnit

/**
 * 官方表情包提供者
 * @property relativePath String
 */
class EmoteGroupsProvider private constructor() : AbstractNgaResourceProvider<BbsCodeBody, List<EmoteGroup>>(
    javaType = TYPE_FACTORY.constructCollectionLikeType(List::class.java, EmoteGroup::class.java),
    expires = TimeUnit.DAYS.toSeconds(1)
) {
    override val relativePath = "$DIR/emotes.json"

    /**
     * 请求方法
     * @param resourceApi NgaApi
     * @return Call<NgaResponse<R>>
     */
    override fun requestMethod(resourceApi: ResourceApi) = resourceApi.bbsCode()

    /**
     * 将响应类型转换为缓存类型的方法
     * @param res R
     * @return D
     */
    override fun transform(res: BbsCodeBody?) = res?.emoteGroup

    companion object {
        private var instance: WeakReference<EmoteGroupsProvider> = WeakReference(EmoteGroupsProvider())
        fun getInstance(): EmoteGroupsProvider = instance.get() ?: EmoteGroupsProvider().also { instance = WeakReference(it) }
    }
}